/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */

package cn.webrx;

import java.util.concurrent.TimeUnit;

/**
 * <p>Project: jse2303 - SleepDemo</p>
 * <p>Powered by webrx On 2023-07-27 11:53:25</p>
 * <p>描述：<p>
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class SleepDemo {

    Object o = new Object();

    void show() {
        String t = Thread.currentThread().getName();
        System.out.println(t + "启动");
        if ("S".equals(t)) {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        synchronized (o) {
            try {
                if ("B".equals(t)) {
                    Thread.sleep(2000);
                } else if ("S".equals(t)) {
                    //o.notify();
                    o.notifyAll(); //不释放锁
                    TimeUnit.SECONDS.sleep(10);
                    System.out.println("nonono");
                } else {
                    //o.wait();//释放锁
                    o.wait(8000);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(t + "结束");
        }
    }


    public static void main(String[] args) {
        var t = new SleepDemo();
        var a = new Thread(t::show, "A");
        var b = new Thread(t::show, "B");
        var c = new Thread(t::show, "C");
        a.start();
        b.start();
        c.start();

        var s = new Thread(t::show, "S");
        s.start();


        //sleep(long n);
        //System.out.printf("%tF %<tT%n",System.currentTimeMillis());
        //try{
        //    //Thread.sleep(5000);
        //    //TimeUnit.SECONDS.sleep(2);
        //          System.out.println("ok");
        //}catch(Exception e){
        //    e.printStackTrace();
        //}
        //System.out.printf("%tF %<tT%n",System.currentTimeMillis());
    }
}
